package com.hbk.controller;

import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.hbk.base.common.CommonResult;
import com.hbk.base.common.Ref;
import com.hbk.base.component.EnvPostProcessor;
import com.hbk.base.component.ExternalConfig;
import com.hbk.base.util.StringUtil;
import com.hbk.service.IOssService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/img")
public class ImageController {

    @Resource
    IOssService ossService;

    @RequestMapping("/upload")
    public CommonResult upload(HttpServletRequest request){
        if(!(request instanceof MultipartHttpServletRequest)){
            System.out.println("不是文件上传");
            return Ref.error("不是文件上传");
        }
        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> multiFileMap = req.getMultiFileMap();
        if(multiFileMap==null || multiFileMap.size()==0){
            return Ref.error("上传文件为空");
        }
        MultipartFile file = multiFileMap.getFirst("file");
        if(file==null){
            return Ref.error("上传文件为空");
        }

        String item = request.getParameter("item");
        if(StringUtil.isEmpty(item)){
            item = request.getHeader("item");
        }
        if(StringUtil.isEmpty(item)){
            item = "image";
        }

        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        DefaultIdentifierGenerator generator = new DefaultIdentifierGenerator();
        Long id = generator.nextId(item);
        LocalDate now = LocalDate.now();

        StringBuilder builder = new StringBuilder();
        builder.append(item);
        builder.append("/");
        builder.append(now.getYear());
        builder.append("/");
        builder.append(now.getMonthValue());
        builder.append("/");
        builder.append(id);
        builder.append(originalFilename.substring(i));
        String key = builder.toString();
        String visitUrl = "";
        try {
            visitUrl = ossService.put(key, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String,String> map = new HashMap<>();
        map.put("id","101");
        map.put("imgUrl",visitUrl);
        map.put("prefix", visitUrl);
        map.put("name", originalFilename);
        return Ref.ok(map);
    }


    public CommonResult uploadOld(HttpServletRequest request){
        if(!(request instanceof MultipartHttpServletRequest)){
            System.out.println("不是文件上传");
            return Ref.error("不是文件上传");
        }
        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> multiFileMap = req.getMultiFileMap();
        if(multiFileMap==null || multiFileMap.size()==0){
            return Ref.error("上传文件为空");
        }
        MultipartFile file = multiFileMap.getFirst("file");
        if(file==null){
            return Ref.error("上传文件为空");
        }

        String item = request.getParameter("item");
        if(StringUtil.isEmpty(item)){
            item = request.getHeader("item");
        }
        if(StringUtil.isEmpty(item)){
            item = "image";
        }

        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        int i = originalFilename.lastIndexOf(".");
        String fileName = StringUtil.randomName() + originalFilename.substring(i);
        ExternalConfig externalConfig = EnvPostProcessor.getExternalConfig();
        String imgDir = externalConfig.getEditor().getImgDir();
        String imgPrefix = externalConfig.getEditor().getImgPrefix();
        String filePath = imgDir + "/"+ item +"/" + fileName;
        File imgFile = new File(filePath);
        File parentFile = imgFile.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        try {
            file.transferTo(imgFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String imgUrl = "/upload/"+ item +"/" + fileName;
        Map<String,String> map = new HashMap<>();
        map.put("id","101");
        map.put("imgUrl",imgUrl);
        map.put("prefix", imgPrefix);
        return Ref.ok(map);
    }
}
